package com.phkj.qdh.ui.fragment.teaminspect

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.commit
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.phkj.qdh.databinding.FragmentTeamInspectSelectBinding
import com.phkj.qdh.ui.activity.TeamInspectActivity
import com.phkj.qdh.ui.adapter.TeamSortAdapter
import com.phkj.qdh.utils.MyItemDecoration
import com.phkj.qdh.utils.launchAndRepeatWithViewLifecycle
import kotlinx.coroutines.flow.collectLatest

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 执法巡查选择页面
 */
class TeamInspectSelectFragment : Fragment() {
    private var _binding: FragmentTeamInspectSelectBinding? = null
    private val binding get() = _binding!!
    private var param1: String? = null
    private var param2: String? = null
    private val teamSortAdapter by lazy { TeamSortAdapter() }
    private lateinit var vm: TeamInspectVM

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
        requireActivity().onBackPressedDispatcher.addCallback(this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    parentFragmentManager.popBackStack()
                }
            })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentTeamInspectSelectBinding.inflate(inflater, container, false)
        vm = ViewModelProvider(this, getTeamInspectVMProvider())[TeamInspectVM::class.java]
        return binding.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.run {
            rvTeamType.run {
                layoutManager = LinearLayoutManager(requireContext())
                adapter = teamSortAdapter
                addItemDecoration(MyItemDecoration(32))
            }
            ivBack.setOnClickListener {
                parentFragmentManager.popBackStack("team", FragmentManager.POP_BACK_STACK_INCLUSIVE)
            }
        }

        teamSortAdapter.setOnItemClickListener { adapter, _, pos ->
            val intent = Intent(requireActivity(), TeamInspectActivity::class.java)
            startActivity(intent)
        }

        launchAndRepeatWithViewLifecycle {
            vm.teamSortList.collectLatest {
                teamSortAdapter.submitList(it)
            }
        }

        teamSortAdapter.setOnItemClickListener { adapter, _, pos ->
            val data = adapter.getItem(pos)
            data?.let {
                parentFragmentManager.commit {
                    addToBackStack("team")
                    replace(id, TeamInspectSubmitFragment.newInstance(it, ""))
                    setReorderingAllowed(true)
                }
            }

        }

        getSort()
    }

    private fun getSort() {
        vm.getTeamSort(3)
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String, param2: String) = TeamInspectSelectFragment().apply {
            arguments = Bundle().apply {
                putString(ARG_PARAM1, param1)
                putString(ARG_PARAM2, param2)
            }
        }
    }
}